package com.xbts.screen.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.xbts.common.core.enums.WorkshopTypeEnum;
import com.xbts.common.core.utils.DateUtils;
import com.xbts.common.core.web.domain.BaseHistogramResult;
import com.xbts.common.core.web.domain.BasePieChartResult;
import com.xbts.screen.domain.dto.HistogramDto;
import com.xbts.screen.domain.workshop.dto.CommonQueryDTO;
import com.xbts.screen.domain.workshop.vo.*;
import com.xbts.screen.mapper.FinalInspectionMapper;
import com.xbts.screen.service.IFinalInspectionService;
import com.xbts.screen.service.IWorkshopCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 终检
 */
@Service
public class FinalInspectionServerImpl implements IFinalInspectionService {

    @Autowired
    private FinalInspectionMapper finalInspectionMapper;
    @Autowired
    private IWorkshopCommonService workshopCommonService;

    @Override
    public RealTimeAchievementRateVO realTimeAchievementRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.realTimeAchievementRate(dto);
    }

    @Override
    public BaseHistogramResult tenDaysAchievementRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.tenDaysAchievementRate(dto);
    }

    @Override
    public StatisticalRateGroupByTimeVO achievementRateGroupByTimeType() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.achievementRateGroupByTimeType(dto);
    }

    @Override
    public BaseHistogramResult waterUnitConsume() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.waterUnitConsume(dto);
    }

    @Override
    public BaseHistogramResult electricityUnitConsume() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.electricityUnitConsume(dto);
    }

    @Override
    public List<ProductionScheduleVO> productionSchedule() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.productionSchedule(dto);
    }

    @Override
    public BaseHistogramResult turnoverRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.turnoverRate(dto);
    }

    @Override
    public StatisticalRateGroupByTimeVO turnoverRateGroupByTimeType() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.turnoverRateGroupByTimeType(dto);
    }

    @DS("wecan")
    @Override
    public SamplingPassRateVO samplingPassRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        List<String> todayDate = DateUtils.getLast30DaysRange();
        dto.setStartTime(todayDate.get(0));
        dto.setEndTime(todayDate.get(1));
        SamplingPassRateVO vo = finalInspectionMapper.samplingPassRate(dto);
        vo.setPassRate(String.format("%.2f", Double.parseDouble(vo.getPassRate())));
        vo.setDefectRate(String.format("%.2f", Double.parseDouble(vo.getDefectRate())));
        return vo;
    }

    @DS("wecan")
    @Override
    public BaseHistogramResult samplingPassRateTrend() {
        CommonQueryDTO dto = new CommonQueryDTO();
        List<String> time = DateUtils.getLast21DaysRange();
        dto.setStartTime(time.get(0));
        dto.setEndTime(time.get(1));
        List<HistogramDto> dtoList = finalInspectionMapper.samplingPassRateTrend(dto);
        BaseHistogramResult result = new BaseHistogramResult();
        List<String> last21Days = DateUtils.getLast21Days();
        result.setXAxis(last21Days);
        List<String> target = new ArrayList<>();
        List<String> actual = new ArrayList<>();
        result.setTarget(target);
        result.setActual(actual);
        for (String day : last21Days) {
            target.add("100");
            String actualStr = "100";
            for (HistogramDto histogramDto : dtoList) {
                if (DateUtils.parseDateToStr(DateUtils.MM_DD, DateUtils.parseDate(histogramDto.getXAxis())).equals(day)){
                    actualStr = String.format("%.2f", Double.parseDouble(histogramDto.getActual()));
                    break;
                }
            }
            actual.add(actualStr);
        }

        return result;
    }

    @DS("wecan")
    @Override
    public List<BasePieChartResult> samplingBadStatistics() {
        CommonQueryDTO dto = new CommonQueryDTO();
        List<String> time = DateUtils.getLast30DaysRange();
        dto.setStartTime(time.get(0));
        dto.setEndTime(time.get(1));
        List<BasePieChartResult> resultList = finalInspectionMapper.samplingBadStatistics(dto);
        return workshopCommonService.getBasePieChartResults(resultList);
    }

    @Override
    public PassRateVO inspectionPassRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.passRate(dto);
    }

    @Override
    public BaseHistogramResult inspectionPassRateTrend() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.passRateTrend(dto);
    }

    @Override
    public List<BasePieChartResult> inspectionBadStatistics() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.badStatistics(dto);
    }

    @Override
    public ScrapRateVO PCS_scrapPassRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.scrapRate(dto);
    }

    @Override
    public List<BasePieChartResult> PCS_scrapStatistics() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.scrapStatistics(dto);
    }

    @Override
    public List<PassRateRecordVO> passRateRecord() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.FINAL_INSPECTION.getErpValue().split(",")));
        return workshopCommonService.passRateRecord(dto);
    }
}